<?php
// This file holds functions needed for various language issues.
// It is designed as if it is a class - at first I wanted to make
// a class, but it needs no instantiation and PHP doesn't have static 
// variables, I am counting on programmers' discipline. 

// DO NOT USE THE ARRAYS DIRECTLY! Their design WILL change!
// Use the functions, or write your own functions if neither the
// existing ones are inappropriate.

// $inst_lang_data         = array( "it", "es", "en", "fr", "de");
// $uninst_lang_data       = array();
// $lang_dir_data          = array();
// $lang_forum_data        = array();
// $phpbblangs             = array();
// $locales                = array( "en"=>"en_US",);
// $lang_name_data         = array();
// $eng_name_data          = array(); 

// require_once $relPath."helpers.php";
// require_once $relPath."iso_lang_list.inc";
// require_once $relPath.'iso_639_list.inc';   // 2-char codes and names
// require_once $relPath.'iso_639_loc_list.inc';

/*
function langcode_2($code)
{
    return $iso_639_1_list_[$code];
    if(strlen($code) == 2)
        return $code;
    else if(strlen($code) == 3 && isset($iso_639_1_list_[$code]))
        return $iso_639_1_list_[$code];
    else
        return left($code, 2);
}
*/

/*
function installed_langs() 
{
    global $inst_lang_data;
    return $inst_lang_data;
	// return $GLOBALS['inst_lang_data'];
}
*/

// Returns an array of languages not installed on the
// site, but whose is planned. Languages with "strange" codes
// (hy, br, cy, ...) are those without gettext() support.
// function uninstalled_langs()
// {
	// global $uninst_lang_data;
	// return $uninst_lang_data;
// }

/*
function lang_code($shortcode) 
{
	global $locales;

	if( isset($locales[$shortcode]) )
        return $locales[$shortcode];
    $shortcode = substr($shortcode, 0, 5);
	if( isset($locales[$shortcode]) )
        return $locales[$shortcode];
    $shortcode = substr($shortcode, 0, 2);
	return( isset($locales[$shortcode]) )
        ? $locales[$shortcode]
        : false;
}

function short_lang_code($langcode = FALSE)
{
    global $Context;
    if(! $langcode)
        $langcode = $Context->DefaultLocale();
    return substr($langcode, 0, 2);
    // return locale_get_primary_language($langcode);
}

// Returns ISO 639 code(s) for language(s) of a project.
// $which could be "primary", "secondary" or "all":
// - "primary" returns the code of primary language
// - "secondary" the code of secondary language, or NULL if the
//   secondary language doesn't exist
// - "all" returns code(s) of both language(s); if the secondary
//   language doesn't exist, the array only has one element. If a
//   language doesn't have a code, NULL is returned.

function proj_lang_code($project, $which)
{
	global $iso_639;

    list($pri_code, $sec_code) = $project->LanguageCodes();

	switch($which) 
    {
	case "primary":
        return $iso_639[$pri_code];
		
	case "secondary":
        return $iso_639[$sec_code];
	
	case "all":
        return array($iso_639[$pri_code],$iso_639[$sec_code]);  
	}
}

function LanguageArray()
{
    
}

function LanguageName($code)
{
    global $iso_language_codes;
    return @$iso_language_codes[$code];
}

function lang_name($langcode)
{
	global $iso_639_loc, $lang_name_data;
	return isset($lang_name_data[$langcode]) 
        ? $lang_name_data[$langcode]
	    : $iso_639_loc[substr($langcode,0,2)];
}

function eng_name($langcode)
{
	global $iso_639, $eng_name_data;
    return isset($eng_name_data[$langcode])
        ? $eng_name_data[$langcode]
	    : $iso_639[substr($langcode,0,2)];
}

function bilingual_name($langcode)
{
	$a = eng_name($langcode);
	$b = lang_name($langcode);

	if($a == $b || ! $b) 
    {
		return $a;
	}
    else if(!$a) 
    {
		return $b;
	}
    else 
    {
		return "$b ($a)";
	}
}

function lang_direction($langcode = null)
{
    global $Context;
    if(! $langcode)
        $langcode = $Context->DefaultLocale();

	switch(short_lang_code($langcode))
    {
	        case "ar":
	        case "fa":
	        case "he":
	        case "ur": //surely more
                return "rtl";
	        default:
                return "ltr";
	}
}

function lang_dir($langcode = FALSE)
{
    global $Context;
	global $lang_dir_data;

    if(! $langcode)
        $langcode = $Context->DefaultLocale();

	$a = short_lang_code($langcode);

    return (isset($lang_dir_data[$a]))
        ? $a."/"
        : "";
}

// This function returns forum of a language. If there is no such
// forum, it can return whatever is appropriate (or nothing,
// of course).
function lang_forum($langcode = FALSE)
{
	global $lang_forum_data;
    global $Context;

    if(! $langcode)
        $langcode = $Context->DefaultLocale();

	return (isset($lang_forum_data[$langcode])) 
        ? $lang_forum_data[$langcode]
        : false;
}

function phpbb_lang($langcode = FALSE) 
{
	global $phpbblangs;
    global $Context;

    if(! $langcode)
        $langcode = $Context->DefaultLocale();

	return $phpbblangs[$langcode];
}
*/

function lang_html_header($langcode = FALSE)
{
    global $Context;

    return "";

    if(! $langcode)
        $langcode = $Context->DefaultLocale();

    $dir = @lang_direction($langcode);
    return empty($dir)
        ? " lang='".short_lang_code($langcode)."'"
        : " lang='".short_lang_code($langcode)." dir=$dir'";
}

?>
